package com.mobimagic.lockscreen.view;

import java.util.Observable;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.Log;
import android.view.View;

import com.mobimagic.lockscreen.AppEnv;
import com.mobimagic.lockscreen.Charge;
import com.mobimagic.lockscreen.Charge.ChargingValueType;
import com.mobimagic.lockscreen.MobileChargingHelper;
import com.mobimagic.lockscreen.R;
import com.mobimagic.lockscreen.font.TypeFaceManager;
import com.mobimagic.lockscreen.sdk.LockScreenSDK;
import com.mobimagic.lockscreen.util.ScreenUtil;
import com.mobimagic.lockscreen.view.MobileChargingScrollView.OnMobileScrollViewScroll;
import com.mobimagic.lockscreen.view.MobileChargingScrollView.ScrollInfo;
import com.mobimagic.lockscreen.view.MobileChargingView.CardType;

public class ChargingProgress implements OnMobileScrollViewScroll {
    private static final boolean DEBUG = AppEnv.bAppdebug;
    private static final String TAG = "ChargingProgress";
    private static final float MIDLINE_COLOR_PERCENT = 0.6f;
    private Context mContext;
    private Charge charge;
    private int width, height;
    private float radius;
    private float drawWidth;
    private float baseLineY;
    private float scale = 1;
    private float alphaValue = 1;
    private int topGap;
    /**
     * 圈与圈之间的连线
     */
    private float lineLength;
    private float oX1, oX2, oX3, oY;
    private float circleMarinBottom;
    private float left;
    private float midLineY;
    private float midLineLeftX;
    /**
     * 百分比下面的横线宽度
     */
    private float midLineWidth;
    private int textSize1, textSize2, textSize3, textSize4, textSize5, textSize7;
    private int marginBottom, marginLeft, marginTop, marginRight, marginRight2, paintWidth;

    private String remaining, speed, continuous, trickle, maintanence, finish, needCharge, hour, minute;
    private Typeface fontFaceThin, fontFaceLight, fontFaceRegular;

    // sunruijian: 3个圆形进度的资源
    private RectF oval_1, oval_2, oval_3;

    private float progress_1, progress_2, progress_3;
    private Bitmap bitmap_1, bitmap_2, bitmap_3;

    AlphaPaint line_1, line_2, paintCircleBc, paintCircleFiller_1, paintCircleFiller_2, paintCircleFiller_3,
            bitmapPaint1, bitmapPaint2, bitmapPaint3, paintCircle_1, paintCircle_2, paintCircle_3;
    /**
     * TYPE_PRO_X 配合setCirclePro方法使用,传入对应type改变对应圆环属性.
     */
    public static final int TYPE_PRO_1 = 1;
    public static final int TYPE_PRO_2 = 2;
    public static final int TYPE_PRO_3 = 3;

    public static final int TYPE_LINE_1 = 1;
    public static final int TYPE_LINE_2 = 2;

    private float progressTextHeight;
    private float progressAlpha = 1;
    private boolean isSingleCardShowAll = false;

    /**
     */
    private SimpleStaticLayout mobileChargingStaticLayout;

    int[] hourAndMinute = new int[2];

    public void setBounds(int width, int height) {
        this.width = width;
        this.height = height;
        calculatOY();
        left = (width - radius * 6 - 2 * lineLength) / 2;
        oX1 = left + radius;
        oX2 = oX1 + 2 * radius + lineLength;
        oX3 = oX2 + 2 * radius + lineLength;

        midLineY = height - LayoutData.getInstance().getLineBottom();
        midLineLeftX = (width - midLineWidth) / 2;

    }

    public ChargingProgress(float drawWidth, float radus, float lineLength, float marginBottom, float lineWidth,
            Paint paint) {
        this.mContext = LockScreenSDK.getInstance().getContext();
        colorHelper = MobileChargingHelper.getInstance().getColorHelper();
        this.radius = radus;
        this.lineLength = lineLength;
        this.drawWidth = drawWidth;
        this.circleMarinBottom = marginBottom;
        this.midLineWidth = lineWidth;

        paintWidth = ScreenUtil.dpToPxInt(mContext, 2);
        paintCircle_1 = new AlphaPaint();
        paintCircle_1.setStrokeCap(Paint.Cap.ROUND);
        paintCircle_1.setStyle(Paint.Style.STROKE);
        paintCircle_1.setStrokeWidth(paintWidth);
        paintCircle_1.setAntiAlias(true);

        paintCircle_2 = new AlphaPaint();
        paintCircle_2.setStrokeCap(Paint.Cap.ROUND);
        paintCircle_2.setStyle(Paint.Style.STROKE);
        paintCircle_2.setStrokeWidth(paintWidth);
        paintCircle_2.setAntiAlias(true);

        paintCircle_3 = new AlphaPaint();
        paintCircle_3.setStrokeCap(Paint.Cap.ROUND);
        paintCircle_3.setStyle(Paint.Style.STROKE);
        paintCircle_3.setStrokeWidth(paintWidth);
        paintCircle_3.setAntiAlias(true);

        paintCircleFiller_1 = new AlphaPaint();
        paintCircleFiller_1.setAntiAlias(false);

        paintCircleFiller_2 = new AlphaPaint();
        paintCircleFiller_2.setAntiAlias(false);

        paintCircleFiller_3 = new AlphaPaint();
        paintCircleFiller_3.setAntiAlias(false);

        paintCircleBc = new AlphaPaint();
        paintCircleBc.setStrokeCap(Paint.Cap.ROUND);
        paintCircleBc.setStyle(Paint.Style.STROKE);
        paintCircleBc.setStrokeWidth(paintWidth);
        paintCircleBc.setAntiAlias(true);

        bitmapPaint1 = new AlphaPaint();
        bitmapPaint2 = new AlphaPaint();
        bitmapPaint3 = new AlphaPaint();

        line_1 = new AlphaPaint();
        line_1.setStrokeWidth(paintWidth);
        line_1.setColor(speedLineColor);

        line_2 = new AlphaPaint();
        line_2.setStrokeWidth(paintWidth);
        line_2.setColor(speedLineColor);

        remaining = LockScreenSDK.getInstance().getString(R.string.remaining);
        speed = LockScreenSDK.getInstance().getString(R.string.speed);
        continuous = LockScreenSDK.getInstance().getString(R.string.continuous);
        trickle = LockScreenSDK.getInstance().getString(R.string.trickle);
        maintanence = LockScreenSDK.getInstance().getString(R.string.maintanence);
        needCharge = LockScreenSDK.getInstance().getString(R.string.plz_charge);
        finish = LockScreenSDK.getInstance().getString(R.string.charge_finish);
        hour = LockScreenSDK.getInstance().getString(R.string.avai_time_hour);
        minute = LockScreenSDK.getInstance().getString(R.string.avai_time_minute);

        paint.setTextSize(textSizeSp12);
        float textWidth = lineLength + 2 * radus;
        speedSimpleStaticLayout = new SimpleStaticLayout(paint, speed, textWidth);
        continuousSimpleStaticLayout = new SimpleStaticLayout(paint, continuous, textWidth);
        trickleSimpleStaticLayout = new SimpleStaticLayout(paint, trickle, textWidth);

        FontMetrics fontMetrics = new FontMetrics();
        paint.getFontMetrics(fontMetrics);
        progressTextHeight = fontMetrics.descent - fontMetrics.ascent;

        init(paint);
    }

    private void init(Paint paint) {
        textSize1 = ScreenUtil.sp2px(mContext, 72);
        textSize2 = ScreenUtil.sp2px(mContext, 24);
        textSize3 = ScreenUtil.sp2px(mContext, 12);
        textSize4 = ScreenUtil.sp2px(mContext, 24);
        textSize5 = ScreenUtil.sp2px(mContext, 14);
        textSize7 = ScreenUtil.sp2px(mContext, 20);

        marginBottom = ScreenUtil.dpToPxInt(mContext, 12);
        marginLeft = ScreenUtil.dpToPxInt(mContext, 8);
        marginTop = ScreenUtil.dpToPxInt(mContext, 6);
        marginRight = ScreenUtil.dpToPxInt(mContext, 10);
        marginRight2 = ScreenUtil.dpToPxInt(mContext, 45);
        progressMarginTop = ScreenUtil.dpToPxInt(mContext, 16);
        percentLineWidth = ScreenUtil.dpToPxInt(mContext, 1);
        textSizeSp12 = ScreenUtil.sp2px(mContext, 12);

        fontFaceThin = TypeFaceManager.getInstance().getTypeface(TypeFaceManager.FONT_ROBOTO_THIN, Typeface.NORMAL);
        fontFaceLight = TypeFaceManager.getInstance().getTypeface(TypeFaceManager.FONT_ROBOTO_ROBOTO_LIGHT,
                Typeface.NORMAL);
        fontFaceRegular = TypeFaceManager.getInstance().getTypeface(TypeFaceManager.FONT_ROBOTO_REGULAR,
                Typeface.NORMAL);
        paint.setTextSize(textSize7);
        String charging = LockScreenSDK.getInstance().getString(R.string.mobile_charging);
        if (charging != null) {
            charging = charging.toUpperCase();
        } else {
            charging = "";
        }
        mobileChargingStaticLayout = new SimpleStaticLayout(paint, charging, ScreenUtil.dpToPx(mContext, 120));

    }

    public void setBitmaps(Bitmap b1, Bitmap b2, Bitmap b3) {
        bitmap_1 = b1;
        bitmap_2 = b2;
        bitmap_3 = b3;
    }

    /**
     * @param type
     *            TYPE_PRO_1 : 第一个圆环
     *            TYPE_PRO_2 : 第二个圆环
     *            TYPE_PRO_1 : 第三个圆环
     * @param currentPro
     *            圆环当前进度 : [0, 100]对应0% - 100%
     * @param progressColor
     *            圆环外环颜色
     * @param fillColor
     *            圆环填充颜色 : 传入-1为空心透明圆环, 传入具体颜色值则圆环被颜色填充.
     * @param lineColor
     *            圆环背景和横线的颜色
     * @param b
     *            圆环内icon的bitmap
     */
    public void setCirclePro(int type, int currentPro, int progressColor, int fillColor, int lineColor, Bitmap b,
            int bitmapAlpha) {
        switch (type) {
        case TYPE_PRO_1: {
            setProgress1(currentPro);
            paintCircle_1.setColor(progressColor);
            if (fillColor == -1) {
                paintCircleFiller_1.setStyle(Paint.Style.STROKE);
            } else {
                paintCircleFiller_1.setStyle(Paint.Style.FILL_AND_STROKE);
                paintCircleFiller_1.setColor(fillColor);
            }
            if (b != null) {
                bitmap_1 = b;
            }
            bitmapPaint1.setAlpha(bitmapAlpha);
            paintCircleBc.setColor(lineColor);
            break;
        }
        case TYPE_PRO_2: {
            setProgress2(currentPro);
            paintCircle_2.setColor(progressColor);
            if (fillColor == -1) {
                paintCircleFiller_2.setStyle(Paint.Style.STROKE);
            } else {
                paintCircleFiller_2.setStyle(Paint.Style.FILL_AND_STROKE);
                paintCircleFiller_2.setColor(fillColor);
            }
            if (b != null) {
                bitmap_2 = b;
            }
            bitmapPaint2.setAlpha(bitmapAlpha);
            paintCircleBc.setColor(lineColor);
            break;
        }
        case TYPE_PRO_3: {
            setProgress3(currentPro);
            paintCircle_3.setColor(progressColor);
            if (fillColor == -1) {
                paintCircleFiller_3.setStyle(Paint.Style.STROKE);
            } else {
                paintCircleFiller_3.setStyle(Paint.Style.FILL_AND_STROKE);
                paintCircleFiller_3.setColor(fillColor);
            }
            if (b != null) {
                bitmap_3 = b;
            }
            bitmapPaint3.setAlpha(bitmapAlpha);
            paintCircleBc.setColor(lineColor);
            break;
        }
        default:
            break;
        }
    }

    /**
     * @param color_1
     *            line_1的颜色
     * @param color_2
     *            line_2的颜色
     */
    public void setLineColor(int type, int color) {
        switch (type) {
        case TYPE_LINE_1: {
            line_1.setColor(color);
            break;
        }
        case TYPE_LINE_2: {
            line_2.setColor(color);
            break;
        }
        default:
            break;
        }
    }

    public void setCharge(Charge charge) {
        this.charge = charge;
        if (speedLineColor != 0 && speedLineColor != 0) {
            generateColors();
        }
    }

    private static final int RING_INNER_COLOR_FULL = 0XFF69da30;
    private static final int PROGRESS_COLOR = 0XFFFFFFFF;
    /**
     * 进度文字离圆的距离
     */
    private int progressMarginTop;
    private SimpleStaticLayout speedSimpleStaticLayout;
    private SimpleStaticLayout continuousSimpleStaticLayout;
    private SimpleStaticLayout trickleSimpleStaticLayout;
    private int percentLineWidth;
    private ScrollInfo scrollInfo;
    private int textSizeSp12;
    private MobileChargeColorHelper colorHelper;
    private int precentColor;
    private int circleInnerColor;
    private int speedLineColor;

    private void generateColors() {
        if (charge == null) {
            return;
        }
        float curValue = charge.getChargingValue();
        Bitmap b = null;
        int value1, value2, value3;
        value1 = (int) (100 * (Math.min(curValue / Charge.SPEED_MAX, 1)));
        value2 = (int) (100 * (Math.max((curValue - Charge.SPEED_MAX) / (1 - Charge.SPEED_MAX), 0)));
        if (curValue < 1) {
            value3 = 0;
        } else {
            long fullTime = System.currentTimeMillis() - charge.getFullTime();
            float value = (fullTime * 1f / Charge.TRICKLE_TIME);
            value = Math.max(0, value);
            value = Math.min(1, value);
            value3 = (int) (value * 100);
        }

        int innerColor = 0;
        int btmColor1 = 255;
        int btmColor2 = value1 == 100 ? 255 : 100;
        int btmColor3 = value2 == 100 ? 255 : 100;
        switch (colorHelper.getColorTheme()) {
        case Red:
            innerColor = value1 == 100 ? RING_INNER_COLOR_FULL : circleInnerColor;

            setCirclePro(ChargingProgress.TYPE_PRO_1, value1, PROGRESS_COLOR, innerColor, speedLineColor, b, btmColor1);
            innerColor = value2 == 100 ? RING_INNER_COLOR_FULL : circleInnerColor;
            setCirclePro(ChargingProgress.TYPE_PRO_2, value2, PROGRESS_COLOR, innerColor, speedLineColor, b, btmColor2);
            innerColor = value3 == 100 ? RING_INNER_COLOR_FULL : circleInnerColor;
            setCirclePro(ChargingProgress.TYPE_PRO_3, value3, PROGRESS_COLOR, innerColor, speedLineColor, b, btmColor3);
            break;
        case Yellow:
            innerColor = value1 == 100 ? RING_INNER_COLOR_FULL : circleInnerColor;

            setCirclePro(ChargingProgress.TYPE_PRO_1, value1, PROGRESS_COLOR, innerColor, speedLineColor, b, btmColor1);
            innerColor = value2 == 100 ? RING_INNER_COLOR_FULL : circleInnerColor;
            setCirclePro(ChargingProgress.TYPE_PRO_2, value2, PROGRESS_COLOR, innerColor, speedLineColor, b, btmColor2);
            innerColor = value3 == 100 ? RING_INNER_COLOR_FULL : circleInnerColor;
            setCirclePro(ChargingProgress.TYPE_PRO_3, value3, PROGRESS_COLOR, innerColor, speedLineColor, b, btmColor3);
            break;
        case Blue:
            innerColor = value1 == 100 ? RING_INNER_COLOR_FULL : circleInnerColor;
            setCirclePro(ChargingProgress.TYPE_PRO_1, value1, PROGRESS_COLOR, innerColor, speedLineColor, b, btmColor1);
            innerColor = value2 == 100 ? RING_INNER_COLOR_FULL : circleInnerColor;
            setCirclePro(ChargingProgress.TYPE_PRO_2, value2, PROGRESS_COLOR, innerColor, speedLineColor, b, btmColor2);
            innerColor = value3 == 100 ? RING_INNER_COLOR_FULL : circleInnerColor;
            if (value3 < 100) {
                setCirclePro(ChargingProgress.TYPE_PRO_3, value3, PROGRESS_COLOR, innerColor, speedLineColor, b,
                        btmColor3);
            } else {
                setCirclePro(ChargingProgress.TYPE_PRO_3, value3, PROGRESS_COLOR, innerColor, speedLineColor, b,
                        btmColor3);

            }

            break;

        default:
            break;
        }

    }

    public void draw(Canvas canvas, Paint paint, Wave wave1, Wave wave2) {
        canvas.save();
        canvas.scale(scale, scale, width / 2, baseLineY);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(drawWidth);
        int alpha = (int) (alphaValue * 255);
        paint.setColor(Color.WHITE);
        paint.setAlpha(alpha);
        drawProgress(canvas, paint);
        canvas.restore();
        int sc = canvas
                .saveLayer(0, 0, width, height, null, Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG
                        | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG
                        | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
        drawLineAndPercent(canvas, paint);
        drawProgressText(canvas, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
        wave1.drawW(canvas, paint);
        wave2.drawW(canvas, paint);
        paint.setXfermode(null);
        canvas.restoreToCount(sc);
        paint.setAlpha(255);
    }

    private void drawProgress(Canvas canvas, Paint paint) {
        if (!charge.isCharging()) {
            return;
        }
        // 根据当前状态改变圆环背景和线的颜色.
        if (progress_1 != 100) {
            line_1.setColor(speedLineColor);
            line_2.setColor(speedLineColor);
            paintCircleBc.setColor(speedLineColor);
        } else if (progress_1 == 100 && progress_2 != 100) {
            line_1.setColor(PROGRESS_COLOR);
            line_2.setColor(speedLineColor);
            paintCircleBc.setColor(speedLineColor);
        } else if (progress_1 == 100 && progress_2 == 100) {
            line_1.setColor(PROGRESS_COLOR);
            line_2.setColor(PROGRESS_COLOR);
            paintCircleBc.setColor(speedLineColor);
        }
        setPaintAlphas();
        canvas.drawLine(oX1 + radius, oY, oX2 - radius, oY, line_1.widthColorAlpha());
        canvas.drawLine(oX2 + radius, oY, oX3 - radius, oY, line_2.widthColorAlpha());

        // 设置第1个圆
        if (oval_1 == null) {
            oval_1 = new RectF(oX1 - radius, oY - radius, oX1 + radius, oY + radius);
        } else {
            oval_1.left = oX1 - radius;
            oval_1.top = oY - radius;
            oval_1.right = oX1 + radius;
            oval_1.bottom = oY + radius;
        }
        canvas.drawArc(oval_1, 0, 360, false, paintCircleFiller_1.widthColorAlpha());
        canvas.drawArc(oval_1, 0, 360, false, paintCircleBc.widthColorAlpha());

        canvas.drawBitmap(bitmap_1, (oX1 - radius) + (bitmap_1.getWidth() / 2), (oY - radius)
                + (bitmap_1.getHeight() / 2), bitmapPaint1.widthColorAlpha());
        if (progress_1 != 0) {
            canvas.drawArc(oval_1, -180, (progress_1 / 100 * 360), false, paintCircle_1.widthColorAlpha());
        }

        // 设置第2个圆
        if (oval_2 == null) {
            oval_2 = new RectF(oX2 - radius, oY - radius, oX2 + radius, oY + radius);
        } else {
            oval_2.left = oX2 - radius;
            oval_2.top = oY - radius;
            oval_2.right = oX2 + radius;
            oval_2.bottom = oY + radius;
        }
        canvas.drawArc(oval_2, 0, 360, false, paintCircleFiller_2.widthColorAlpha());
        canvas.drawArc(oval_2, 0, 360, false, paintCircleBc.widthColorAlpha());

        canvas.drawBitmap(bitmap_2, (oX2 - radius) + (bitmap_2.getWidth() / 2), (oY - radius)
                + (bitmap_2.getHeight() / 2), bitmapPaint2.widthColorAlpha());
        if (progress_2 != 0) {
            canvas.drawArc(oval_2, -180, (progress_2 / 100 * 360), false, paintCircle_2.widthColorAlpha());
        }

        // 设置第3个圆
        if (oval_3 == null) {
            oval_3 = new RectF(oX3 - radius, oY - radius, oX3 + radius, oY + radius);
        } else {
            oval_3.left = oX3 - radius;
            oval_3.top = oY - radius;
            oval_3.right = oX3 + radius;
            oval_3.bottom = oY + radius;
        }
        canvas.drawArc(oval_3, 0, 360, false, paintCircleFiller_3.widthColorAlpha());
        canvas.drawArc(oval_3, 0, 360, false, paintCircleBc.widthColorAlpha());

        canvas.drawBitmap(bitmap_3, (oX3 - radius) + (bitmap_3.getWidth() / 2), (oY - radius)
                + (bitmap_3.getHeight() / 2), bitmapPaint3.widthColorAlpha());
        if (progress_3 != 0) {
            canvas.drawArc(oval_3, -180, (progress_3 / 100 * 360), false, paintCircle_3.widthColorAlpha());
        }

    }

    private void setPaintAlphas() {
        line_1.setAnimatAlpha(alphaValue * progressAlpha);
        line_2.setAnimatAlpha(alphaValue * progressAlpha);
        paintCircleBc.setAnimatAlpha(alphaValue * progressAlpha);
        paintCircleFiller_1.setAnimatAlpha(alphaValue * progressAlpha);
        paintCircleFiller_2.setAnimatAlpha(alphaValue * progressAlpha);
        paintCircleFiller_3.setAnimatAlpha(alphaValue * progressAlpha);
        bitmapPaint1.setAnimatAlpha(alphaValue * progressAlpha);
        bitmapPaint2.setAnimatAlpha(alphaValue * progressAlpha);
        bitmapPaint3.setAnimatAlpha(alphaValue * progressAlpha);
        paintCircle_1.setAnimatAlpha(alphaValue * progressAlpha);
        paintCircle_2.setAnimatAlpha(alphaValue * progressAlpha);
        paintCircle_3.setAnimatAlpha(alphaValue * progressAlpha);
    }

    private void drawProgressText(Canvas canvas, Paint paint) {
        if (!charge.isCharging()) {
            return;
        }
        Align align = paint.getTextAlign();

        paint.setTextSize(textSizeSp12);
        float y = oval_1.bottom + progressMarginTop + progressTextHeight;
        paint.setTextAlign(Align.CENTER);
        canvas.save();
        canvas.scale(scale, scale, width / 2, baseLineY);
        paint.setAlpha((int) (255 * alphaValue * progressAlpha));
        speedSimpleStaticLayout.draw(canvas, oval_1.centerX(), y);
        continuousSimpleStaticLayout.draw(canvas, oval_2.centerX(), y);
        trickleSimpleStaticLayout.draw(canvas, oval_3.centerX(), y);
        canvas.restore();
        paint.setTextAlign(align);

    }

    private void drawLineAndPercent(Canvas canvas, Paint paint) {
        int color = getColor();
        paint.setColor(color);

        ChargingValueType chargingValueType = charge.getChargingValueType();
        int currentBattery = Math.round(charge.getChargingValue() * 100);
        paint.setStrokeWidth(drawWidth);
        paint.setStyle(Style.FILL);

        String text = "";
        paint.setAlpha((int) (255 * progressAlpha));
        text = "" + currentBattery;
        paint.setTypeface(fontFaceThin);
        paint.setTextSize(textSize1);
        paint.setTextAlign(Align.CENTER);

        if (charge.isRealValue()) {
            canvas.drawText(text, width / 2, midLineY - marginBottom, paint);
            int marginLeftPercent = (int) paint.measureText(text) / 2 + marginLeft;
            text = "%";
            paint.setTypeface(fontFaceLight);
            paint.setTextSize(textSize2);
            canvas.drawText(text, width / 2 + marginLeftPercent, midLineY - marginBottom, paint);
        } else {
            paint.setTextSize(textSize7);
            paint.setTypeface(null);
            mobileChargingStaticLayout.draw(canvas, width / 2, midLineY - marginBottom);
        }

        if (chargingValueType.equals(ChargingValueType.TrickleComplete)) {
            paint.setAlpha((int) (255 * progressAlpha));
            paint.setTextAlign(Align.CENTER);
            paint.setTypeface(fontFaceRegular);
            paint.setTextSize(textSize3);
            int baseLine = textSize3;
            text = finish;
            canvas.drawText(text, width / 2, midLineY + baseLine, paint);
        } else {
            int alpha = (int) (MIDLINE_COLOR_PERCENT * 255);
            paint.setAlpha((int) (alpha * progressAlpha));
            paint.setStrokeWidth(percentLineWidth);
            canvas.drawLine(midLineLeftX, midLineY, midLineLeftX + midLineWidth, midLineY, paint);

            if (charge.isCharging()) {
                paint.setAlpha((int) (alpha * progressAlpha));
                paint.setTextAlign(Align.LEFT);
                paint.setTypeface(fontFaceRegular);
                paint.setTextSize(textSize3);
                int baseLine = textSize3;
                if (chargingValueType.equals(ChargingValueType.Speed)) {
                    text = speed;
                } else if (chargingValueType.equals(ChargingValueType.Continuous)) {
                    text = continuous;
                } else if (chargingValueType.equals(ChargingValueType.Trickle)) {
                    text = maintanence;
                }
                canvas.drawText(text, midLineLeftX, midLineY + marginTop + baseLine, paint);

                baseLine += textSize3 + 5;
                canvas.drawText(remaining, midLineLeftX, midLineY + marginTop + baseLine, paint);

                paint.setTextAlign(Align.RIGHT);
                int[] times = convertTime(charge.getChargingRemainingTime());

                if (times[0] == 0) {
                    text = times[1] + "";
                    paint.setTextSize(textSize4);
                    canvas.drawText(text, width / 2 + midLineWidth / 2 - marginRight, midLineY + marginTop + baseLine,
                            paint);

                    text = minute;
                    paint.setTextSize(textSize5);
                    int padding = (int) paint.measureText(text);
                    canvas.drawText(text, width / 2 + midLineWidth / 2 - marginRight + padding, midLineY + marginTop
                            + baseLine, paint);
                } else {
                    paint.setTextSize(textSize4);
                    text = times[0] + "";
                    canvas.drawText(text, width / 2 + midLineWidth / 2 - marginRight2, midLineY + marginTop + baseLine,
                            paint);

                    text = hour;
                    paint.setTextSize(textSize5);
                    int padding = (int) paint.measureText(text);
                    canvas.drawText(text, width / 2 + midLineWidth / 2 - marginRight2 + padding, midLineY + marginTop
                            + baseLine, paint);

                    if (times[1] != 0) {
                        text = times[1] + "";
                        paint.setTextSize(textSize4);
                        padding += (int) paint.measureText(text);
                        canvas.drawText(text, width / 2 + midLineWidth / 2 - marginRight2 + padding, midLineY
                                + marginTop + baseLine, paint);

                        text = minute;
                        paint.setTextSize(textSize5);
                        padding += (int) paint.measureText(text);
                        canvas.drawText(text, width / 2 + midLineWidth / 2 - marginRight2 + padding, midLineY
                                + marginTop + baseLine, paint);
                    }
                }
            } else {
                if (currentBattery < 90) {
                    paint.setAlpha((int) (alpha * progressAlpha));
                    paint.setTextAlign(Align.CENTER);
                    paint.setTypeface(fontFaceRegular);
                    paint.setTextSize(textSize3);
                    int baseLine = textSize3;
                    text = needCharge;
                    canvas.drawText(text, width / 2, midLineY + marginTop + baseLine, paint);
                }
            }
        }
    }

    private int getColor() {
        int color = Color.WHITE;
        if (precentColor != 0) {
            color = precentColor;
        }
        return color;
    }

    private int[] convertTime(long time) {
        hourAndMinute[0] = hourAndMinute[1] = 0;
        if (time >= 0) {
            int hour = (int) (time / 60);
            int minute = (int) (time % 60);
            hourAndMinute[0] = hour;
            if (minute != 0) {
                hourAndMinute[1] = minute;
            }
        }
        return hourAndMinute;
    }

    @Override
    public void onScroll(ScrollInfo scrollInfo) {
        this.scrollInfo = scrollInfo;
        if (DEBUG) {
            Log.i(TAG, "onScroll");
        }
        calculatOY();
    }

    private void calculatOY() {
        //head-cover=translateHeight
        if (scrollInfo == null) {
            baseLineY = height;
        } else {
            baseLineY = topGap + (height - topGap) * (1 - scrollInfo.value);
            if (scrollInfo.value <= scrollInfo.coverValue) {
                baseLineY = height;
                alphaValue = scale = 1;
            } else if (scrollInfo.value > scrollInfo.coverValue && scrollInfo.value <= scrollInfo.showHeadValue) {
                float full = scrollInfo.showHeadValue - scrollInfo.coverValue;
                float precent = (scrollInfo.value - scrollInfo.coverValue) / full;
                baseLineY = height - (scrollInfo.translateHeight) * (precent);
                alphaValue = scale = 1;
            } else if (scrollInfo.value > scrollInfo.showHeadValue && scrollInfo.value <= scrollInfo.showAllValue) {
                baseLineY = height - (scrollInfo.translateHeight)
                        - (scrollInfo.translateHeight / (scrollInfo.showHeadValue - scrollInfo.coverValue))
                        * (scrollInfo.value - scrollInfo.showHeadValue) / 2;
                float value = 1 - ((scrollInfo.value - scrollInfo.showHeadValue) / (scrollInfo.showAllValue - scrollInfo.showHeadValue)) - 0.4f;
                if (value > 0) {
                    alphaValue = scale = value;
                } else {
                    alphaValue = scale = 0;
                }
            } else {
                baseLineY = height - (scrollInfo.translateHeight)
                        - (scrollInfo.translateHeight / (scrollInfo.showHeadValue - scrollInfo.coverValue))
                        * (1 - scrollInfo.showHeadValue) / 2;
                alphaValue = scale = 0;
            }
        }
        oY = baseLineY - circleMarinBottom - radius;
    }

    public void setTopGap(int topGap) {
        this.topGap = topGap;
    }

    public float getProgress1() {
        return progress_1;
    }

    public float getProgress2() {
        return progress_2;
    }

    public float getProgress3() {
        return progress_3;
    }

    public void setProgress1(float progress1) {
        this.progress_1 = progress1;
    }

    public void setProgress2(float progress2) {
        this.progress_2 = progress2;
    }

    public void setProgress3(float progress3) {
        this.progress_3 = progress3;
    }

    public void setProgressAlpha(float alpha) {
        progressAlpha = alpha;
    }

    @Override
    public void onRemove(View v, CardType cardType) {

    }

    @Override
    public void onCardShowAll(View v, CardType cardType) {

    }

    @Override
    public void onCardShowHead(View v, CardType cardType) {

    }

    @Override
    public void onOpenAd(View slidingView) {

    }

    public void update(Observable observable, Object data) {
        ChargeColor chargeColor = (ChargeColor) data;
        circleInnerColor = chargeColor.getCircleProInnerCurrentColor();
        speedLineColor = chargeColor.getSpeedLineCurrentColor();
        precentColor = chargeColor.getPrecentTextCurrentColor();
        generateColors();
    }

    public Float getCurrentScale() {
        return scale;
    }

    public RectF getOval_1() {
        return oval_1;
    }

    public RectF getOval_2() {
        return oval_2;
    }

    public RectF getOval_3() {
        return oval_3;
    }
}
